package com.expert.controller;


import com.expert.common.jqgrid.GridResult;
import com.expert.common.page.MyPage;
import com.expert.entity.Feature;
import com.expert.service.FeatureService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import com.expert.common.base.BaseController;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.expert.service.impl.ResultServiceImpl;
import com.expert.entity.Result;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author Mankind
* @since 2018-06-03
*/
@Controller
@RequestMapping("/console/result")
public class ResultController extends BaseController {
    @Autowired
    private ResultServiceImpl resultService;
    @ModelAttribute("result")
    public Result get(@RequestParam(required = false)Long id) throws Exception{
        if(id != null && id > 0){
            return resultService.selectById(id);
        }else{
            return new Result();
        }
    }

    @RequestMapping(value="/list",produces = "text/html;charset=utf-8",method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        return "/admin/result/list";
    }

    @ResponseBody
    @RequestMapping(value="/list.json",produces = "application/json;charset=utf-8")
    public GridResult list() throws Exception{
        MyPage<Result> page = getPage();
        page = resultService.list(page);
        GridResult result = new GridResult(page.getCurrent(),page.getPages(),page.getCurrent(),page.getRecords());
        return result;
    }

    /**
     * 跳转添加/编辑界面
     * @param result
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit", produces = "text/html;charset=utf-8", method = RequestMethod.GET)
    public String add(@ModelAttribute Result result,Model model) throws Exception{
        model.addAttribute("result",result);
        return "/admin/result/edit";
    }

    /**
     * 处理更新的请求
     * @param result
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Result result){
        Map<String,Object> ans = new HashedMap();
        try {
        if(result.getId() != null && result.getId() > 0){
            resultService.updateById(result);
        }else {
            resultService.insert(result);
        }
        ans.put("status",200);
        ans.put("msg","result信息保存成功");
        }catch (Exception e){
        e.printStackTrace();
        ans.put("status",500);
        ans.put("msg","result信息保存失败");
        }
        return ans;
    }

    /**
     * 处理删除的请求
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashedMap();
        try {
        if(id != null && id > 0){
            resultService.deleteById(id);
            result.put("status",200);
            result.put("msg","删除result成功");
        }else {
            result.put("status",500);
            result.put("msg","请选择删除的result");
        }
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除result失败");
        }
        return result;
    }

}
